<?php
class Application_Model_Filter_CouponSearch
{
    /**
     * 快速搜索
     */
    public $quick;
    /**
     * 时间类型
     */
    public $dateType;
    /**
     * 开始时间
     */
    public $startTime;
    /**
     * 结束时间
     */
    public $endTime;
    /**
     * 底线金额
     */
    public $money;
    /**
     * 折扣
     */
    public $discount;
    /**
     * 状态
     */
    public $status;
    /**
     * 类型
     */
    public $type;
    /**
     * 范围
     */
    public $scope;
    /**
     * 用户Id
     */
    public $userId;
    /**
     * 使用类型
     */
    public $useScope;
    /**
     * 使用地方
     */
    public $useType;
    /**
     * 关联ID
     */
    public $relevance;
    /**
     * 优惠券批次
     */
    public $batchNo;
    /**
     * 排序
     */
    public $order;
            
    function getSearchFileds() {
        $where = array();
        $and = "";
        if($this->quick != ""){
            $userItem = Application_Model_User::getByNikeNameStore($this->quick);
            if ($userItem != null) {
                $inuserid = "(";
                foreach ($userItem as $userval) {
                    $inuserid.= $userval->id . ",";
                }
                $inuserid.= "0)";
                $where[$and . " userId in "] = $inuserid;
                $and = " and ";
            } else {
                $where[$and . " no like "] = "%".$this->quick."%";
                $where[" or batchNo like "] = "%".$this->quick."%";
                $where[" or name like "] = "%".$this->quick."%";
                $and = " and ";
            }
        }
        
        if($this->dateType != ""){
            $this->order = $this->dateType." desc ";
        } else {
            $this->order = " dateExpired desc ";
        }
        
        if($this->startTime != ""){
            $where[$and . $this->dateType ." >= "] = $this->startTime;
            $and = " and ";
        }
        if($this->endTime != ""){
            $where[$and . $this->dateType ." <= "] = $this->endTime." 23:59:59";
            $and = " and ";
        }
        if($this->money != ""){
            $where[$and ." money = "] = $this->money;
            $and = " and ";
        }
        if($this->discount != ""){
            $where[$and ." discount = "] = $this->discount;
            $and = " and ";
        }
        if($this->status != ""){
            if($this->status == Application_Model_Coupon::STATUS_EXPIRED){
                $where[$and . "( status = "] = $this->status;
                $where[' or dateExpired <= '] = date("Y-m-d H:i:s");
                $where[" ) and 1 = "] = 1;
                $and = ' and ';
            } else if ($this->status == Application_Model_Coupon::STATUS_OK) {
                $where[$and . "( status = "] = $this->status;
                $where[' and dateCreated <= '] = date("Y-m-d H:i:s");
                $where[' and dateExpired > '] = date("Y-m-d H:i:s");
                $where[" ) and 1 = "] = 1;
                $and = ' and ';
            } else {
                $where[$and . " status = "] = $this->status;
                $and = ' and ';
            }
        }
        if($this->type != ""){
            $where[$and . " type = "] = $this->type;
            $and = ' and ';
        }
        if($this->scope != ""){
            $where[$and . " scope = "] = $this->scope;
            $and = ' and ';
        }
        if($this->userId != ""){
            $where[$and." userId = "] = $this->userId;
            $and = ' and ';
        }
        if($this->useScope != ""){
            if($this->useScope == Application_Model_Coupon::Scope_Waybill){
                $where[$and . " scope in "] = "(".Application_Model_Coupon::Scope_All.",".Application_Model_Coupon::Scope_Waybill.")";
                $and = ' and ';
            } else if ($this->useScope == Application_Model_Coupon::Scope_Order) {
                $where[$and . " scope in "] = "(".Application_Model_Coupon::Scope_All.",".Application_Model_Coupon::Scope_Order.")";
                $and = ' and ';
            }
        }
        if($this->useType != ""){
            $where[$and." useType = "] = $this->useType;
            $and = ' and ';
        }
        if($this->relevance != ""){
            $where[$and." relevance = "] = $this->relevance;
            $and = ' and ';
        }
        if($this->batchNo != ""){
            $where[$and." batchNo = "] = $this->batchNo;
            $and = ' and ';
        }
        return $where;
    }
    
    function getOrder()
    {
        return $this->order;
    }
}